using Identity.Application.Contracts.Common;
using Identity.Application.Contracts.Services;
using Identity.Application.UserAggregates.Commands;
using MediatR;

namespace Identity.Application.UserAggregates.CommandHandler;

public class ConfirmEmailCommandHandler : IRequestHandler<ConfirmEmailCommand, Result>
{
    private readonly IUserService _userService;
    public ConfirmEmailCommandHandler(IUserService userService)
    {
        _userService = userService; 
    }
    public async Task<Result> Handle(ConfirmEmailCommand request, CancellationToken cancellationToken)
    {
        
        var user = await _userService.GetUserByIdAsync(request.Id);
        if (user == null) return Result.Failure("用户不存在");

        var result = await _userService.ConfirmEmailAsync(request.Id, request.token);
        if (!result)
            return Result.Failure("邮箱确认失败");

        return Result.Success();
    }
}
